import * as React from 'react';
import { createStackNavigator, StackNavigator } from '@react-navigation/stack';
import {
  View, Text, ScrollView, ImageBackground, StyleSheet,
  Dimensions, TextInput, TouchableOpacity, AsyncStorage, DeviceEventEmitter
} from 'react-native';
import Textarea from 'react-native-textarea';
import Icon from 'react-native-vector-icons/AntDesign';

const Newlist = ({ navigation, route }) => {
  const [context, onChangecon] = React.useState('');

  // const storeData=async()=>{
  //   let data =await AsyncStorage.getItem('data');
  //   var timestamp = Date.parse(new Date());
  //   var date = new Date(timestamp);
  //   var time=date.getHours()+':' +date.getMinutes();
  //   data =data?JSON.parse(data):[]
  //   data.unshift({context:context,time:time})
  //   await AsyncStorage.setItem('data',JSON.stringify(data))
  //   // navigation.pop('word',{data:{}})
  //   navigation.push('TreeHole',{data:{}})
  // }


  const save = async () => {
    let data = await AsyncStorage.getItem('data');
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    var month = date.getMonth() + 1;
    var time = date.getFullYear() + '年' + month + '月' + date.getDate() + '日' + date.getHours() + ':' + date.getMinutes();
    data = data ? JSON.parse(data) : []
    data.unshift({ context: context, time: time })
    await AsyncStorage.setItem('data', JSON.stringify(data))
    DeviceEventEmitter.emit('refresh', {
      'a': data,
    })

    // AsyncStorage.clear()
    // let data =AsyncStorage.getItem('data');
    // var localdata =data?JSON.parse(data):[]
    // var object = {
    //     context:context,
    //     data:date.getHours()+':' +date.getMinutes()
    // };
    // data.unshift({context:context,time:time})
    console.log(data)
    navigation.goBack();
    //   new Promise((resolve, reject) => {
    //   AsyncStorage.setItem('localdata',JSON.stringify(localdata), (error) => {
    //       if (error) {
    //           // 错误
    //       } else {
    //           // 存储成功
    //           resolve(true);
    //       }
    //   })
    // }).then(() => {
    //    console.log('back');
    //    navigation.goBack();
    //   DeviceEventEmitter.emit('refresh',{
    //     'a':localdata,
    //   })
    //  })
  }
  return (
    <ScrollView>
      <View>
        <ImageBackground style={styles.tu}
          source={require('../../Image/treeHole.jpg')}>
          <TouchableOpacity
            style={styles.goback}
            onPress={() => { navigation.goBack() }}>
            <Icon size={40} name='left' color='#DCDCDC'></Icon>
          </TouchableOpacity>
          <Textarea
            placeholder='别怕~释放自己的情绪吧&#13;&#10;这里只有树洞菌在守护'
            style={styles.input}
            onChangeText={text => onChangecon(text)}
            value={context}>
          </Textarea>
          <TouchableOpacity style={styles.save} onPress={save}>
            <Icon size={40} name='checkcircleo' color='#808080'></Icon>
          </TouchableOpacity>
        </ImageBackground>
      </View>
    </ScrollView>
  )
}
export default Newlist;


const styles = StyleSheet.create({
  tu: {
    position: 'relative',
    flex: 1,
    marginLeft: ptd(0),
    marginRight: ptd(0),
    height: h,

  },
  goback: {
    height: 60,
    width: 80,
    paddingLeft: ptd(10),
    paddingTop: 10,
    // backgroundColor:'pink'   
  },
  input: {
    textAlign: 'center',
    fontSize: 20,
    height: 200,
    marginTop: 0.35 * Dimensions.get('window').height,
    color: '#A0522D'
    // borderColor:'pink',
    // borderWidth:2,
  },

  delete: {
    position: 'absolute',
    // backgroundColor:'pink',
    width: 80,
    height: 60,
    marginLeft: ptd(40),
    alignItems: 'center',
    top: 0.83 * Dimensions.get('window').height,
  },
  save: {
    position: 'absolute',
    // backgroundColor:'pink',
    width: 80,
    height: 60,
    marginLeft: ptd(295),
    alignItems: 'center',
    top: 0.85 * Dimensions.get('window').height
  },

})
